package com.aabte.auth.account.domain.model;

import com.aabte.commons.ddd.shared.ValueObject;
import lombok.Getter;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.regex.Pattern;

@SuppressWarnings("all")
@Embeddable
@Getter
@ToString
public class AccountName implements ValueObject<AccountName> {

    private static final Pattern PATTERN_NAME = Pattern.compile("^[a-zA-Z0-9_-]{4,16}$");

    @Column(name = "account_name", unique = true, nullable = false)
    private String name;

    private AccountName() {
    }

    public AccountName(String name) {
        this.setName(name);
    }

    private void setName(String name) {
        if (!PATTERN_NAME.matcher(name).matches()) {
            throw new IllegalArgumentException("account name incorrect. name: " + name
                    + ", usage: " + PATTERN_NAME.pattern());
        }
        this.name = name;
    }

    @Override
    public boolean sameValueAs(AccountName accountName) {
        return this.name.equals(accountName.name);
    }
}
